/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package marsaMaroc.dao;

import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import marsaMaroc.model.Client;
import marsaMaroc.model.Escale;
import marsaMaroc.model.Facture;
import marsaMaroc.model.Produit;

/**
 *
 * @author Administrateur
 */
public class EscaleDAO extends DAO<Escale>{

    EntityManagerFactory emf = Persistence.createEntityManagerFactory("Marsa-MarocPU") ;
    EntityManager em;
    
    @Override
    public void add(Escale object) { 
        em = emf.createEntityManager();
        em.getTransaction().begin();   
        em.persist(object);
        em.getTransaction().commit();  
    }
 
    public void delete(int id) {
        em = emf.createEntityManager();
        em.getTransaction().begin();   
        Escale o = em.find(Escale.class, id);
        em.remove(o);
        em.getTransaction().commit();
                     
          
    }

    @Override
    public void update(Escale object) {
        em = emf.createEntityManager();
        em.getTransaction().begin();   
        em.merge(object);
        em.getTransaction().commit();
    }

    @Override
    public Escale get(int id) {
          em = emf.createEntityManager();
          Escale t = em.find(Escale.class, id);
          System.out.println("t"+t.getId());
          return t;
    
    }

    @Override
    public List<Escale> getAll() {
        em = emf.createEntityManager();
        Query q = em.createQuery("select e FROM Escale e") ;
        return q.getResultList();
    }
    public List<Produit> chargerListBox() {
        em = emf.createEntityManager();
        Query q = em.createQuery("select e FROM Produit e") ;
        System.out.println("liste produits :"+q.getResultList().size());
        return q.getResultList();
    }

    @Override
    public void delete(Escale object) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }
    
    
    
}
